﻿using BaseData.Exts;
using BaseData.HelperEzui;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// NewsAdminNodeListLower 的摘要说明
    /// </summary>
    public class NewsAdminNodeListLower : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            var items = TreeNodeRoot(context, context.Request.QueryString["id"].ToInt(0));
            context.Response.Write(items.ObjectsToJson());
            context.Response.ContentType = "text/plain";
        }
        private static List<Ezui_Node<Web_Base_News_Node>> TreeNodeRoot(HttpContext context, int reqId)
        {
            using (var ctx = new ContextExt())
            {
                var sql =
                    ctx.Web_Base_News_Node.Where(w => w.ParentId == reqId)
                        .Select(q => new Ezui_Node<Web_Base_News_Node>
                        {
                            //id = q.PkId,
                            PkId = q.PkId,
                            ParentId = q.ParentId,
                            //text = q.Title,
                            Title = q.Title,
                            Folder = q.Folder,
                            //DateTimeCreate = q.DateTimeCreate,
                            attributes = new Attributes { key = q.PkId, value = q.PkId, },
                            FlagStatus = q.FlagStatus,
                            //status = q.FlagStatus ?? 0,
                            //state =
                            //    (!ctx.Web_Base_News_Node.Any(o => o.ParentId == q.PkId) || reqId == 0
                            //        ? Enums.TreeStat.Open
                            //        : Enums.TreeStat.Closed).ToString().ToLower(),
                        });
                return sql.Any() ? sql.ToList() : new List<Ezui_Node<Web_Base_News_Node>>();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}